package com.haoting.oss.controller;


import com.haoting.commonutils.CommonResult;
import com.haoting.oss.service.OssService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/useross/fileoss")
//@CrossOrigin
public class OssController {

    @Autowired
    OssService ossService;

    @PostMapping("/{folder}")
    public CommonResult uploadOssFile(MultipartFile file,@ApiParam(name = "folder",value="图片文件夹",required = true)@PathVariable String folder){
        // MultipartFile 获取上传的文件
        String url = ossService.uploadFileAvatar(file,folder);
        return CommonResult.ok().data("url",url);
    }

    @DeleteMapping("/delete/{folder}/{name}")
    public CommonResult deleteOssfile(@PathVariable("folder") String folder,@PathVariable("name") String name){
        String imagename = folder+"/"+name;
        ossService.deleteFileAvatar(imagename);
        return CommonResult.ok();
    }
}
